<template>
  <!-- 添加或修改跨境通关常见问题对话框 -->
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="1000px"
    append-to-body
    :close-on-click-modal="false"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
             size="small">
      <el-form-item label="问题" prop="issue">
        <el-input v-model.trim="form.issue" type="textarea" placeholder="请输入内容"/>
      </el-form-item>

      <el-form-item label="解答">
        <editor v-model.trim="form.answer" :min-height="192"/>
      </el-form-item>


      <el-form-item label="跨境通关常见问题状态" label-width="200">
        <el-radio-group v-model.trim="form.status">
          <el-radio
            v-for="dict in dict.type.start_stop_status"
            :key="dict.value"
            :label="dict.value"
          >{{ dict.label }}
          </el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="附件">
        <fileUploadMiniApp v-model.trim="form.fileUrl"/>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model.trim="form.remark" placeholder="请输入备注"/>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getIssueInfo, addIssueInfo, updateIssueInfo} from "@/api/miniapp/issueInfo";

export default {
  name: "IssueInfo-addEdit",
  props: {
    dialogType: {
      type: String,
      default: "add",
    },
    rowData: {
      type: Object,
      default: () => {
      },
    },
    dict: {}
  },
  data() {
    return {
      // 弹出层
      dialogVisible: false,
      // 标题
      title: "新增",
      // 表单参数
      form: {
        id: null,
        issue: null,
        answer: null,
        status: "0",
        clicks: null,
        fileUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null

      },
      // 跨境通关常见问题表格数据
      issueInfoList: [],
      // 表单校验
      rules: {}
    };
  },
  methods: {
    open() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        if (this.dialogType == "add") {
          this.title = "新增";
        } else if (this.dialogType == "edit") {
          this.title = "编辑";
          this.loadDetail();
        } else if (this.dialogType == "detail") {
          this.title = "详情";
          this.loadDetail();
        }
      });
    },
    close() {
      this.$refs["form"].resetFields();
      this.dialogVisible = false;
    },

    // 获取详情
    loadDetail() {
      const id = this.rowData.id;
      getIssueInfo(id).then((res) => {
        if (res.code == 200) {
          this.form = res.data;
        }
      });
    },

    // 提交表单
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = {
            ...this.form,
          };
          if (this.dialogType == "add") {
            addIssueInfo(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          } else {
            params.id = this.rowData.id;
            updateIssueInfo(this.form).then(res => {
              if (res.code === 200) {
                this.$modal.msgSuccess("修改成功");
                this.close();
                this.$emit("handleQuery");
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
